﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTGroup_ZheJiang.Models;
using DTGroup_ZheJiang.WebUI.Code;
using System.Data;
using DTGroup_ZheJiang.Business;
using DTGroup.Networking.Framework.Utils.Web;
using DTGroup.Networking.Framework.Utils;

namespace DTGroup_ZheJiang.WebUI.List
{
    public partial class BuildingsStatisticsReport : BasePage
    {
        /// <summary>
        /// 每页显示记录数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 当前索引页
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount;
        public string sqlCondition = string.Empty;
        public DataTable dataSource { get; set; }
        public string tableName;

        public BuildingsStatisticsBO bo = new BuildingsStatisticsBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = 15;//每页条数
                PageIndex = RequestUtil.Get<int>("page", Method.All, 1);//当前页
                TotalCount = 0;//总条数
                if (PageIndex <= 0)
                    PageIndex = 1;

                ((DTGroup_ZheJiang.WebUI.MainMaster)Master).Crumb = "楼宇级统计报表";
                string ReportTime = RequestUtil.GetString("ReportTime");

                if (!string.IsNullOrEmpty(ReportTime))
                {
                    string Options = RequestUtil.GetString("Options");
                    string ddlType = RequestUtil.GetString("ddlType");
                    string ddlCity = RequestUtil.GetString("ddlCity");
                    string[] roundNumber = RequestUtil.GetString("roundNumber").Split(',');

                    sqlCondition = " where 1=1 ";
                    #region 数字范围
                    if (roundNumber.Length >= 2)
                    {
                        if (roundNumber[0] != "")
                        {
                            sqlCondition += " and MRCount>= " + roundNumber[0];
                        }
                        if (roundNumber[1] != "")
                        {
                            sqlCondition += " and MRCount<= " + roundNumber[1];
                        }
                        if (roundNumber[2] != "")
                        {
                            sqlCondition += " and WeakMrCount>= " + roundNumber[2];
                        }
                        if (roundNumber[3] != "")
                        {
                            sqlCondition += " and WeakMrCount<= " + roundNumber[3];
                        }
                        if (roundNumber[4] != "")
                        {
                            sqlCondition += " and AvgMRRsrp>= " + roundNumber[4];
                        }
                        if (roundNumber[5] != "")
                        {
                            sqlCondition += " and AvgMRRsrp<= " + roundNumber[5];
                        }
                    }
                    #endregion
                    tableName = "ZJ2_BudSta_" + getTabName(ddlType, ddlCity);
                    sqlCondition += @" and P_Day=to_date('" + Convert.ToDateTime(ReportTime).ToString("yyyy-MM-dd") + "','yyyy-MM-dd') ";

                    if (Options == "Search")
                    {
                        //dataSource = new DataTable();
                        dataSource = bo.GetBuildingsStatisticsByPage(tableName, PageIndex, PageSize, sqlCondition, out TotalCount);
                    }
                    else if (Options == "Export")
                    {
                        Export();
                    }
                    else
                    {
                        dataSource = new DataTable();
                    }
                }
                else
                {
                    dataSource = new DataTable();
                }
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        public void Export()
        {
            string root = System.Configuration.ConfigurationManager.AppSettings["ExportExcelPath"];
            string fileRootDir = MapPath(root + "/fileReport/");
            string fileName = string.Concat("导出", "_", DateTime.Now.ToString("yyyyMMddHHmmss"), "_", new Random().Next(1, 10000), ".csv");
            string filePath = IOUtil.JoinPath(fileRootDir, fileName);
            List<BuildingsStatistics> list = bo.GetBuildingsStatisticsByWhere(tableName, sqlCondition);

            DTGroup_ZheJiang.Utility.ExcelUtil.ListToCsv<BuildingsStatistics>(list, "日期,城市,建筑物ID,建筑物名称,经度,纬度,栅格总数,弱覆盖栅格数,弱覆盖栅格率,无栅格数,"
            + "无栅格率,MR总数,MR弱覆盖数,MR平均RSRP,MR覆盖率,S_CELL1,S_CELL1 采样点数,S_CELL1 弱覆盖采样点数,S_CELL2,S_CELL2采样点数,S_CELL2弱覆盖采样点数,S_CELL3,"
            + "S_CELL3采样点数,S_CELL3弱覆盖采样点数,是否有室分", filePath, (BuildingsStatistics item) =>
            {
                return string.Concat(item.P_Day, ",", item.City, ",", item.BuildingId, ",", item.BuildingName + "," + item.Longitude, ",", item.Latitude, ",",
                    item.GridCount, ",", item.WeakGridCount, ",", item.WeakGridPercent, ",", item.NoGridCount, ",", item.NoGridPercent
                    , ",", item.MRCount, ",", item.WeakMrCount, ",", item.AvgMRRsrp, ",", item.MRCoverPercent, ",", item.S_CELL1, ",", item.S_CELL1_MRCount,
                    ",", item.S_CELL1_WeakMRCount, ",", item.S_CELL2, ",", item.S_CELL2_MRCount, ",", item.S_CELL2_WeakMRCount, ",", item.S_CELL3,
                    ",", item.S_CELL3_MRCount, ",", item.S_CELL3_WeakMRCount, ",", item.HasInDoor == 0 ? "否" : "是");
            }, System.Text.Encoding.Default);

            base.JsonWriter2(fileName, "/fileReport/" + fileName);
        }
    }
}